package cz.muni.fi.pa165.sme.web.pages.sportActivityRecord;

import java.text.DateFormat;
import java.text.NumberFormat;

import cz.muni.fi.pa165.sme.web.fo.SportActivityRecordFO;
import cz.muni.fi.pa165.sme.web.pages.TemplatePage;
import org.apache.wicket.authorization.Action;
import org.apache.wicket.authroles.authorization.strategies.role.Roles;
import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeAction;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.ResourceModel;

/**
 * @author jirankova
 */
@AuthorizeAction(action = Action.RENDER, roles = { Roles.ADMIN, Roles.USER})
public class RecordDetailPage extends TemplatePage {

	public RecordDetailPage(final SportActivityRecordFO sportActivityRecordFO) {
		this(sportActivityRecordFO, null);
	}

	public RecordDetailPage(final SportActivityRecordFO sportActivityRecordFO, final String error) {
		//initialize formatters
		final NumberFormat numberFormat = NumberFormat.getNumberInstance(getLocale());
		final DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, getLocale());

		add(new Label("title", new ResourceModel("title")));

		//check if this page was called with an error message or not; if not error is not visible
		Label errorMessage;
		if (error == null || error.isEmpty()) {
			errorMessage = new Label("error", "");
			errorMessage.setVisible(false);
		} else {
			errorMessage = new Label("error", new ResourceModel(error));
			errorMessage.setVisible(true);
		}
		add(errorMessage);

		//labels and values
		add(new Label("userNameLabel", new ResourceModel("label.user.name")));
		if(sportActivityRecordFO.getUser() != null) {
			add(new Label("userName", sportActivityRecordFO.getUser().getLastName() + ", "+sportActivityRecordFO.getUser().getFirstName()));
		} else {
			add(new Label("userName", ""));
		}

		add(new Label("sportActivityLabel", new ResourceModel("sportActivityLabel")));
		add(new Label("sportActivity", sportActivityRecordFO.getSportActivity().getTitle()));

		add(new Label("activityStartLabel", new ResourceModel("activityStartLabel")));
		add(new Label("activityStart", dateFormat.format(sportActivityRecordFO.getActivityStart())));

		add(new Label("activityStopLabel", new ResourceModel("activityStopLabel")));
		add(new Label("activityStop", dateFormat.format(sportActivityRecordFO.getActivityStop())));

		add(new Label("distanceLabel", new ResourceModel("distanceLabel")));
		add(new Label("distance", numberFormat.format(sportActivityRecordFO.getDistance())));


		add(new Link<SportActivityRecordFO>("edit") {
			@Override
			public void onClick() {
				setResponsePage(new RecordEditPage(sportActivityRecordFO));
			}
		}.add(new Label("editLabel", new ResourceModel("editRecord"))));
	}
}
